package ar.com.solidussource.howmuchmobile.activities.generic;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import ar.com.solidussource.howmuchmobile.R;
import ar.com.solidussource.howmuchmobile.activities.generic.BaseActivity;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 22/11/12
 * Time: 15:37
 * To change this template use File | Settings | File Templates.
 */
public abstract class MenuActivity extends BaseActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = (View) inflater.inflate(R.layout.menu_list_gen, null);

        TextView title = (TextView) v.findViewById(R.id.title);
        title.setText(getMenuOptionTitle());

        ListView lstOpciones = (ListView) v.findViewById(R.id.menuList);

        lstOpciones.setOnItemClickListener(new MenuOnItemClickListener());

        lstOpciones.setAdapter(new MenuArrayAdapter(this));

        setContent(v);
    }

    class MenuArrayAdapter extends ArrayAdapter<String> {

        private Activity context;

        MenuArrayAdapter(Activity context) {
            super(context, R.layout.menu_list_gen_row, getMenuOptions());
            this.context = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View item = inflater.inflate(R.layout.menu_list_gen_row, null);

            TextView lblTitulo = (TextView) item.findViewById(R.id.title);
            lblTitulo.setText(getMenuOptions()[position]);

            ImageView img = (ImageView) item.findViewById(R.id.img);

            img.setImageResource(getMenuOptionIcons()[position]);

            return(item);
        }

    }

    class MenuOnItemClickListener implements AdapterView.OnItemClickListener {

        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            onMenuOptionClick(position);
        }
    }

    public abstract String getMenuOptionTitle();
    public abstract String[] getMenuOptions();
    public abstract Integer[] getMenuOptionIcons();
    public abstract void onMenuOptionClick(int position);
}